package com.zr.controller;


import com.zr.dto.MenuDTO;
import com.zr.interfaces.IStaffService;
import com.zr.pojo.Result;
import com.zr.utils.POIUtils;
import com.zr.vo.StaffVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: peterChen
 * @date: 2019/11/14 20:46
 * @description: com.zr.controller
 * @version: 1.0
 */
@Slf4j
@RestController
//@RequestMapping("api")
public class StaffController {
    @Autowired
    private IStaffService staffService;

    /**
     * 单个添加员工信息
     * @param staffVO
     * @return
     */
    @RequestMapping("addStaff")
    @PreAuthorize("hasAuthority('admin')")
    public Result addStaff(@RequestBody StaffVO staffVO){
        if(staffVO == null){
            return Result.serviceFail("serviceFail");
        }
        staffService.addStaff(staffVO);
        return Result.success("添加成功");
    }


    @RequestMapping("upload")
    @PreAuthorize("hasAuthority('admin')")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

            try {
                //判断文件是否为空，是否为excel文件
                POIUtils.checkFile(excelFile);
                //读取Excel文件数据
                 List<String[]> list = POIUtils.readExcel(excelFile);
                 staffService.add(list);
            } catch (IOException e) {
                e.printStackTrace();
                return Result.serviceFail("新增文件类型异常");
            }
            return Result.success("批量新增成功");
        }

    /**
     * 获取菜单  未完成
     * @param roleId
     * @return
     */
    @RequestMapping("getAllMenus")
    @PreAuthorize("isAuthenticated()")
    public Result getMenus(@RequestParam(name = "roleId",required = false) Integer roleId){
        List<MenuDTO> menuDTOS = staffService.getAllMenus(roleId);
        if(CollectionUtils.isEmpty(menuDTOS)){
            return Result.serviceFail("获取菜单失败，请重新加载");
        }
        return Result.success("获取菜单成功");
    }


}
